

namespace Simnova.Collaborate.WinForm
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Simnova.Collaborate.Presenter;
    using Simnova.Collaborate.View;

    public partial class Admin : Form, ICompany, IProject, IProduct
    {
        private CompanyPresenter companyPresenter;
        private ProjectPresenter projectPresenter;
        private ProductPresenter productPresenter;

        private int userId;

        public Admin(int userId)
        {
            this.userId = userId;
            this.InitializeComponent();
        }

        #region ICompany Members

        int ICompany.CompanyId
        {
            get
            {
                int companyId;
                int.TryParse(this.companyIdTextBox.Text, out companyId);
                return companyId;
            }

            set
            {
                this.companyIdTextBox.Text = value.ToString();
            }
        }

        string ICompany.CompanyName
        {
            get
            {
                return this.companyNameTextBox.Text;
            }

            set
            {
                this.companyNameTextBox.Text = value;
            }
        }

        System.Collections.IList ICompany.CompanyList
        {
            set { this.companyGridView.DataSource = value; }
        }

        #endregion

        #region IProject Members

        int IProject.ProjectId
        {
            get
            {
                return int.Parse(projectIdTextBox.Text);
            }

            set
            {
                projectIdTextBox.Text = value.ToString();
            }
        }

        int IProject.CompanyId
        {
            get
            {
                return int.Parse(projectCompanyIdTextBox.Text);
            }

            set
            {
                projectCompanyIdTextBox.Text = value.ToString();
            }
        }

        string IProject.ProjectName
        {
            get
            {
                return projectNameTextBox.Text;
            }

            set
            {
                projectNameTextBox.Text = value;
            }
        }

        string IProject.ProjectDescription
        {
            get
            {
                return projectDescriptionTextBox.Text;
            }

            set
            {
                projectDescriptionTextBox.Text = value;
            }
        }

        System.Collections.IList IProject.ProjectList
        {
            set { projectGridView.DataSource = value; }
        }

        #endregion

        private void companyGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.companyGridView.SelectedRows.Count > 0)
            {
                int companyId = ((Simnova.Collaborate.Presenter.AppServer.Company)companyGridView.SelectedRows[0].DataBoundItem).CompanyId;
                this.companyPresenter.LoadCompany(companyId);
                this.projectPresenter = new ProjectPresenter(this, companyId);
                this.productPresenter = new ProductPresenter(this, companyId);
            }
        }

        private void Admin_Load(object sender, EventArgs e)
        {
            this.companyPresenter = new CompanyPresenter(this, this.userId);
        }

        private void updateCompanyButton_Click(object sender, EventArgs e)
        {
            this.companyPresenter.Save();
        }

        private void updateProjectButton_Click(object sender, EventArgs e)
        {
            this.projectPresenter.Save();
        }

        #region IProject Members





        #endregion

        #region IProduct Members

        string IProduct.Name
        {
            get
            {
                return productNameTextBox.Text;
            }

            set
            {
                productNameTextBox.Text = value;
            }
        }

        string IProduct.Description
        {
            get
            {
                return this.productDescriptionTextBox.Text;
            }

            set
            {
                this.productDescriptionTextBox.Text = value;
            }
        }

        System.Collections.IList IProduct.ProductList
        {
            set { this.companyProductsGridView.DataSource = value; }
        }

        #endregion

        private void updateProductButton_Click(object sender, EventArgs e)
        {
            this.productPresenter.Save();
        }

        #region IProduct Members

        int IProduct.ProductId
        {
            get
            {
                int productId;
                int.TryParse(productIdTextBox.Text, out productId);
                return productId;
            }

            set
            {
                productIdTextBox.Text = value.ToString();
            }
        }

        #endregion

        private void companyProductsGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.companyProductsGridView.CurrentRow != null)
            {
                this.productPresenter.LoadProduct(((Simnova.Collaborate.Presenter.AppServer.Product)companyProductsGridView.CurrentRow.DataBoundItem).ProductId);
            }
        }
    }
}